package com.mycjj.android.db;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import android.database.Cursor;

public class CityAreaTable extends Tables {
	public static final String TABLE_NAME = "area";
	
	// BaseColumns _id
	public static final String FIELD_AREA_ID = "area_id";

	public static final String FIELD_AREA_NAME = "area_name";

	public static final String FIELD_AREA_PARENT_ID = "area_parent_id";

	public static final String FIELD_AREA_SORT = "area_sort";

	public static final String FIELD_AREA_SHOW = "area_show";

	public static final String FIELD_AREA_DEEP = "area_deep";

	public static final String FIELD_AREA_INITIAL = "area_initial";

	public static final String[] TABLE_COLUMNS = new String[] { _ID, FIELD_AREA_ID, FIELD_AREA_NAME, FIELD_AREA_PARENT_ID, FIELD_AREA_SORT, FIELD_AREA_SHOW, FIELD_AREA_DEEP, FIELD_AREA_INITIAL };

	
	public static final String TABLE_NAME2 = "city";
	public static final String FIELD_CITY_ABBR = "abbr";
	public static final String FIELD_CITY_ID = "id";
	public static final String FIELD_CITY_NAME="city_name";
	
	static CityBaseDao<?> baseDao = (CityBaseDao<?>)CityBaseDao.getInstance();
	public static Map<String, Integer>  getProvince(){
		Map<String, Integer> provinceMap = new LinkedHashMap<String, Integer>();
		String sql = "select "+FIELD_AREA_ID+","+FIELD_AREA_NAME+" from "+TABLE_NAME+" where "+FIELD_AREA_PARENT_ID+" = 0;";
		Cursor cursor = null;
		try{
			cursor = baseDao.rawQuery(sql,null);
		}catch(Exception e){
			e.printStackTrace();
		}
		if (cursor != null ) {
			while(cursor.moveToNext()) {
				provinceMap.put(cursor.getString(1), cursor.getInt(0));
			}
		}
		if(cursor != null && !cursor.isClosed()){
			cursor.close();
		}
		return provinceMap;
	}
	
	public static Map<String,Integer> getPlatProvince(){
		Map<String, Integer> provinceMap = new LinkedHashMap<String, Integer>();
		String sql = "select "+FIELD_CITY_ABBR+" from "+TABLE_NAME2+" group by "+FIELD_CITY_ABBR+";";
		Cursor cursor = null;
		try{
			cursor = baseDao.rawQuery(sql,null);
		}catch(Exception e){
			e.printStackTrace();
		}
		if (cursor != null ) {
			while(cursor.moveToNext()) {
				provinceMap.put(cursor.getString(0), 0);
			}
		}
		if(cursor != null && !cursor.isClosed()){
			cursor.close();
		}
		return provinceMap;
	}
	
	/**
	 * 
	 * @param provinceId 省份id
	 * @return
	 */
	public static Map<String, Integer> getCity(int provinceId) {
		Map<String, Integer> cityMap = new LinkedHashMap<String, Integer>();
		String sql = "select "+FIELD_AREA_ID+","+FIELD_AREA_NAME+" from "+TABLE_NAME+" where "+FIELD_AREA_PARENT_ID+" = "+provinceId+";";
		Cursor cursor = null;
		try{
			cursor = baseDao.rawQuery(sql,null);
		}catch(Exception e){
			e.printStackTrace();
		}
		if (cursor != null) {
			while(cursor.moveToNext()) {
				cityMap.put(cursor.getString(1), cursor.getInt(0));
			}
		}
		if (cursor != null && !cursor.isClosed()) {
			cursor.close();
		}
		return cityMap;
	}
	
	
	public static Map<String,Integer> getPlateCity(String abbr){
		Map<String, Integer> cityMap = new LinkedHashMap<String, Integer>();
		String sql = "select "+FIELD_CITY_NAME+","+FIELD_CITY_ID+" from "+TABLE_NAME2+" where "+FIELD_CITY_ABBR+" = '"+abbr+"';";
		Cursor cursor = null;
		try{
			cursor = baseDao.rawQuery(sql,null);
		}catch(Exception e){
			e.printStackTrace();
		}
		if (cursor != null) {
			while(cursor.moveToNext()) {
				cityMap.put(cursor.getString(0), cursor.getInt(1));
			}
		}
		if (cursor != null && !cursor.isClosed()) {
			cursor.close();
		}
		return cityMap;
	}
	
	public static Map<String, Integer> getArea(int cityId) {
		Map<String, Integer> areaMap = new LinkedHashMap<String, Integer>();
		String sql = "select "+FIELD_AREA_ID+","+FIELD_AREA_NAME+" from "+TABLE_NAME+" where "+FIELD_AREA_PARENT_ID+" = "+cityId+";";
		Cursor cursor = null;
		try{
			cursor = baseDao.rawQuery(sql,null);
		}catch(Exception e){
			e.printStackTrace();
		}
		if (cursor != null ) {
			while(cursor.moveToNext()) {
				areaMap.put(cursor.getString(1), cursor.getInt(0));
			}
		}
		if (cursor != null && !cursor.isClosed()) {
			cursor.close();
		}
		return areaMap;
	}
	
	public static int getCityId(String cityName){
		String sql = "select "+FIELD_AREA_ID+" from "+TABLE_NAME+" where "+FIELD_AREA_NAME+" like '%"+cityName+"%';";
		Cursor cursor = null;
		try{
			cursor = baseDao.rawQuery(sql,null);
		}catch(Exception e){
			e.printStackTrace();
		}
		List<Integer> integerArray = new ArrayList<Integer>();
		if(cursor != null){
			while(cursor.moveToNext()){
				integerArray.add(cursor.getInt(0));
			}
		}
		int size = integerArray.size();
		if(size == 0 || size < 0){
			return 0;
		}else{
			return integerArray.get(size - 1);
		}
		
	}
}
